package com.neusoft.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.neusoft.bean.PageBean;
import com.neusoft.mapper.CheckMapper;
import com.neusoft.service.CheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class CheckServiceImpl implements CheckService {

    @Autowired
    private CheckMapper checkMapper;

    @Override
    public PageBean getList(Integer page, Integer pageSize, String realName, String checkTime) {
        PageHelper.startPage(page,pageSize);
        Page<Map<String,Object>> pageInfo = checkMapper.selectList(realName, checkTime);
        PageBean pageBean = new PageBean();
        List<Map<String, Object>> collect = pageInfo.getResult().stream().map(map -> {
            Object checkTime1 = map.get("check_time");
            if(checkTime1 != null){
                LocalDateTime visitDate = (LocalDateTime)map.get("check_time");
                map.put("check_time", visitDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                return map;
            }
            return map;
        }).collect(Collectors.toList());
        pageBean.setRows(collect);
        pageBean.setCount(pageInfo.getTotal());
        return pageBean;
    }

    @Override
    public Map<String, Object> getCheckDetail(Integer id) {
        Map<String, Object> checkMap = checkMapper.getCheckDetail(id);
        return checkMap;
    }
}
